import threading
from time import sleep

lock = threading.RLock()
# 第一种方法创建多线程
Number = 10


def func(number):
    lock.acquire()
    global Number
    sleep(0.01)
    Number += 1
    print("我的run函数", threading.current_thread().getName(), number, Number)
    lock.release()  # 死锁 HTTP 有个超时时间


for i in range(10):
    t1 = threading.Thread(target=func, args=(i,))  # 另开子线程
    t1.setName("我是{}线程".format(i))
    t1.start()

# # t1.setDaemon(True)
# # t1.join()
# # 在此等待2秒 阻塞主线程作用  不传参数时，阻塞到子线程运行结束，再往下运行  传参数：如果小于线程运行时间，时间一到继续往下执行，
# # 大于线程运行时间，则线程运行结束也继续往下运行
# # t1 已运行4s 离打印还有6s  打印出来  t1结束
# # t2 开始运行 离打印还有10s  4s ->打印出来  主要看CPU繁忙
# t2 = threading.Thread(target=run)
# t2.setName("我是T2线程")
# t2.start()
#
# # t2.join()
#
# """
# self._target = target
# self._target()
# run()
#         try:
#             if target:
#                 self._target(*self._args, **self._kwargs) # target()->run()
#                 run(i)
#         finally:
#             # Avoid a refcycle if the thread is running a function with
#             # an argument that has a member that points to the thread.
#             del self._target, self._args, self._kwargs  # 清理垃圾
#
# """
#
# # sleep(3)
# print("我是主线程")


# 第二种情况 面向对象思想

# class MyThread(threading.Thread):
#     def run(self) -> None:
#         sleep(2)
#         print('我是面向对象的实现多线程', self._args, self._kwargs)
#
#
# MyThread(args=(1,)).start()
#
print("我是主线程！！")
